import os
from jinja2 import Environment
from dataclasses import asdict
from typing import Dict, List
from ..models.vue_meta_info import VueTableMetaInfo, VueColumnMetaInfo, create_vue_meta_info
from ..models.sql_table_info import TableInfoSQL, TableInfoConfig
from src.base import bera

def generate_vue3_antd_code(vue_meta: VueTableMetaInfo, templates: Environment):
    """生成Vue3代码"""
    # 生成组件
    generate_list_view(vue_meta, templates)
    generate_form_view(vue_meta, templates)
    generate_detail_view(vue_meta, templates)

    # 生成API
    generate_api(vue_meta, templates)

    # 生成类型定义
    generate_types(vue_meta, templates)

    # 生成路由
    generate_router(vue_meta, templates)

def generate_list_view(vue_meta: VueTableMetaInfo, templates: Environment):
    """生成列表页面"""
    template = templates.get_template('vue3-antd/List.vue.j2')
    output = template.render(
        class_name=vue_meta.class_name,
        entity_name=vue_meta.entity_name,
        chinese_name=vue_meta.chinese_name,
        search_columns=vue_meta.search_columns,
        table_columns=vue_meta.list_columns,
        version=vue_meta.version,
        current_date=vue_meta.current_date
    )

    # 写入文件
    file_path = os.path.join(bera.get_variable('home_directory'), f'src/logic/auto/{vue_meta.entity_name}/List.vue')
    os.makedirs(os.path.dirname(file_path), exist_ok=True)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(output)
    bera.logger.info(f"Generated Vue List component at {file_path}")

def generate_form_view(vue_meta: VueTableMetaInfo, templates: Environment):
    """生成新增/编辑组件"""
    template = templates.get_template('vue3-antd/AddOrEdit.vue.j2')
    output = template.render(
        class_name=vue_meta.class_name,
        entity_name=vue_meta.entity_name,
        chinese_name=vue_meta.chinese_name,
        form_columns=vue_meta.form_columns,
        version=vue_meta.version,
        current_date=vue_meta.current_date
    )

    file_path = os.path.join(bera.get_variable('home_directory'), f'src/logic/auto/{vue_meta.entity_name}/AddOrEdit.vue')
    os.makedirs(os.path.dirname(file_path), exist_ok=True)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(output)
    bera.logger.info(f"Generated Vue AddOrEdit component at {file_path}")

def generate_detail_view(vue_meta: VueTableMetaInfo, templates: Environment):
    """生成详情组件"""
    template = templates.get_template('vue3-antd/Detail.vue.j2')
    output = template.render(
        class_name=vue_meta.class_name,
        entity_name=vue_meta.entity_name,
        chinese_name=vue_meta.chinese_name,
        detail_columns=vue_meta.list_columns,
        version=vue_meta.version,
        current_date=vue_meta.current_date
    )

    file_path = os.path.join(bera.get_variable('home_directory'), f'src/logic/auto/{vue_meta.entity_name}/Detail.vue')
    os.makedirs(os.path.dirname(file_path), exist_ok=True)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(output)
    bera.logger.info(f"Generated Vue Detail component at {file_path}")

def generate_api(vue_meta: VueTableMetaInfo, templates: Environment):
    """生成API封装"""
    template = templates.get_template('vue3-antd/api.ts.j2')
    output = template.render(
        class_name=vue_meta.class_name,
        entity_name=vue_meta.entity_name,
        api_prefix=f'/api/{vue_meta.entity_name}',
        version=vue_meta.version,
        current_date=vue_meta.current_date
    )

    file_path = os.path.join(bera.get_variable('home_directory'), f'src/logic/auto/{vue_meta.entity_name}/api.ts')
    os.makedirs(os.path.dirname(file_path), exist_ok=True)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(output)
    bera.logger.info(f"Generated Vue API file at {file_path}")

def generate_types(vue_meta: VueTableMetaInfo, templates: Environment):
    """生成TypeScript类型定义"""
    template = templates.get_template('vue3-antd/types.ts.j2')
    output = template.render(
        class_name=vue_meta.class_name,
        columns=vue_meta.columns,
        version=vue_meta.version,
        current_date=vue_meta.current_date
    )

    file_path = os.path.join(bera.get_variable('home_directory'), f'src/logic/auto/{vue_meta.entity_name}/{vue_meta.class_name}.ts')
    os.makedirs(os.path.dirname(file_path), exist_ok=True)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(output)
    bera.logger.info(f"Generated TypeScript types at {file_path}")

def generate_router(vue_meta: VueTableMetaInfo, templates: Environment):
    """生成路由"""
    template = templates.get_template('vue3-antd/router.ts.j2')
    output = template.render(
        class_name=vue_meta.class_name,
        entity_name=vue_meta.entity_name,
        version=vue_meta.version,
        current_date=vue_meta.current_date
    )

    file_path = os.path.join(bera.get_variable('home_directory'), f'src/logic/auto/{vue_meta.entity_name}/router.ts')
    os.makedirs(os.path.dirname(file_path), exist_ok=True)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(output)
    bera.logger.info(f"Generated Vue router at {file_path}")